import requests
from requests import Response

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from yat.test import macro


class DataMigrationApi:
    def __init__(self, token):
        self.headers = None
        self.log = Logger()
        self.comm_sh = CommonSH()
        self.token = token
        self.ip = f"https://{macro.DATAKIT_HOST}:{macro.DATAKIT_PORT}"

    def get_data_migration_resource(self, db_type: str = '') -> Response:
        """
        获取源端或目标端数据库列表
        :param db_type: 数据库类型（sourceClusters/targetClusters）
        :return:
        """
        api = f'/plugins/data-migration/resource/{db_type}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def post_plugins_data_migration_transcribeReplay_save(self, data: dict) -> Response:
        """
        创建录制回放任务
        :param data: 录制回放任务信息
        :return: 响应结果包含录制回放任务id
        """
        api = '/plugins/data-migration/transcribeReplay/save'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, json=data, headers=self.headers, verify=False)
        return response

    def post_plugins_data_migration_transcribeReplay_downloadAndConfig(self, task_id: int, data: dict) -> Response:
        """
        配置录制回放任务并下载录制回放工具
        :param task_id: 任务id
        :param data: 配置信息
        :return:
        """
        api = f"/plugins/data-migration/transcribeReplay/downloadAndConfig?id={task_id}"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, json=data, headers=self.headers, verify=False)
        return response

    def get_plugins_data_migration_transcribeReplay_list(self, page_num: int = 1, page_size: int = 10,
                                                         task_name: str = None, execution_status: str = None,
                                                         task_start_time: str = None,
                                                         task_end_time: str = None) -> Response:
        """
        查看/刷新录制回放任务列表
        :param page_num: 页数
        :param page_size: 每页最大任务数
        :param task_name: 任务名
        :param execution_status: 任务状态
        :param task_start_time: 任务启动时间
        :param task_end_time: 任务结束时间
        :return:
        """
        api = f"/plugins/data-migration/transcribeReplay/list?pageNum={page_num}&pageSize={page_size}"
        if task_name is not None:
            api += f'&taskName={task_name}'
        if execution_status is not None:
            api += f'&executionStatus={execution_status}'
        if task_start_time is not None:
            api += f'&taskStartTime={task_start_time}'
        if task_end_time is not None:
            api += f"&taskEndTime={task_end_time}"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response

    def post_plugins_data_migration_transcribeReplay_start(self, task_id: int):
        """
        启动录制回放任务
        :param task_id: 任务id
        :return:
        """
        api = f"/plugins/data-migration/transcribeReplay/start/{task_id}"
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        requests.post(url=url, headers=self.headers, verify=False)

    def post_plugins_data_migration_transcribeReplay_delete(self, task_id: str) -> Response:
        """
        删除录制回放任务
        :param task_id:任务id，多个任务id以逗号分隔，例如：task_id = "1，2，3"
        :return:
        """
        api = f'/plugins/data-migration/transcribeReplay/delete/{task_id}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.post(url=url, headers=self.headers, verify=False)
        return response

    def get_plugins_data_migration_transcribeReplay_getHostInfo(self, transcribe_task_id: int) -> Response:
        """
        选择录制任务，获取任务信息
        :param transcribe_task_id:录制任务id
        :return: 源端目标端信息
        """
        api = f'/plugins/data-migration/transcribeReplay/getHostInfo/{transcribe_task_id}'
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + self.token
        }
        url = self.ip + api
        response = requests.get(url=url, headers=self.headers, verify=False)
        return response
